package com.net.xpay.core.manager.impl.safePay;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractVersionedManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.safePay.SafePayOrderMapper;
import com.net.xpay.core.domain.safePay.SafePayOrder;
import com.net.xpay.core.enums.SafePayOrderStatus;
import com.net.xpay.core.helper.EnvironmentHelper;
import com.net.xpay.core.manager.safePay.SafePayOrderManager;
import com.net.xpay.core.model.SafePayOrderSearchModel;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Component
public class SafePayOrderManagerImpl extends AbstractVersionedManager<SafePayOrder> implements SafePayOrderManager{

    @Autowired
    private SafePayOrderMapper mapper;

    @Autowired
    private SerialNumberService serialNumberService;

    @Autowired
    private EnvironmentHelper environmentHelper;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(SafePayOrder obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        //外部交易号
        obj.setOutBizNo(environmentHelper.getEnv() + "-" + obj.getUserId() + "-" + obj.getId());
        return super.save(obj);
    }


    @Override
    protected BasicMapper<Long, SafePayOrder> getMapper() {
        return mapper;
    }

    @Override
    public int batchSave(List<SafePayOrder> safePayOrders) {
        if (CollectionUtils.isEmpty(safePayOrders)) {
            return 0;
        }
        for (SafePayOrder trans : safePayOrders) {
            LocalDateTime now = LocalDateTime.now();
            trans.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
            //外部交易号
            trans.setOutBizNo(environmentHelper.getEnv() + "-" + trans.getUserId() + "-" + trans.getId());
            trans.setCreateTime(now);
            trans.setModifyTime(now);
            trans.setVersion(0L);
        }
        return mapper.batchSave(safePayOrders);
    }

    @Override
    public int updateStatusByBatchId(Long userId, Long batchId, SafePayOrderStatus status, Boolean free, LocalDateTime endTime) {
        Preconditions.checkArgument(userId != null && batchId != null && status != null);
        return mapper.updateStatusByBatchId(userId, batchId, status, free, endTime);
    }

    @Override
    public List<SafePayOrder> listByBatchId(Long userId, Long batchId) {
        Preconditions.checkArgument(userId != null && batchId != null);
        SafePayOrder condition = new SafePayOrder();
        condition.setUserId(userId);
        condition.setSafePayBatchId(batchId);
        return listByCondition(condition, new ArrayList<>());
    }

    @Override
    public List<SafePayOrder> search(SafePayOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public List<SafePayOrder> list(SafePayOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }

    @Override
    public List<Long> listIds(SafePayOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        List<Long> ids = mapper.listIds(searchModel);
        return ids == null ? new ArrayList<>() : ids;
    }

    @Override
    public SafePayOrder findById(Long userId, Long id) {
        Preconditions.checkArgument(userId != null && id != null);
        SafePayOrder condition = new SafePayOrder();
        condition.setId(id);
        condition.setUserId(userId);
        return getByCondition(condition);
    }

    @Override
    public SafePayOrder findByImportBizNo(Long userId, String importBatchNo, String importBizNo) {
        Preconditions.checkArgument(userId != null && !Strings.isNullOrEmpty(importBatchNo) && !Strings.isNullOrEmpty(importBizNo));
        SafePayOrder condition = new SafePayOrder();
        condition.setUserId(userId);
        condition.setImportBatchNo(importBatchNo);
        condition.setImportBizNo(importBizNo);
        return getByCondition(condition);
    }

    @Override
    public long statSingleVeryCount(LocalDateTime minEndTime, LocalDateTime maxEndTime, SafePayOrderStatus status) {
        Preconditions.checkArgument(status != null);
        return mapper.statSingleVeryCount(minEndTime, maxEndTime,status);
    }

    @Override
    public long statUserSingleVeryCount(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime, SafePayOrderStatus status) {
        Preconditions.checkArgument(userId != null && minEndTime != null && maxEndTime != null && status != null);
        return mapper.statUserSingleVeryCount(userId,minEndTime, maxEndTime,status);
    }

    @Override
    public long statSafePayTransPayeeAlipayAccount(LocalDateTime minEndTime, LocalDateTime maxEndTime) {

        return mapper.statSafePayTransPayeeAlipayAccount(minEndTime, maxEndTime);
    }

    @Override
    public long statUserLargeCount(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime, SafePayOrderStatus status) {
        Preconditions.checkArgument(status != null);
        return mapper.statUserLargeCount(userId,minEndTime, maxEndTime,status);
    }

    @Override
    public long searchCount(SafePayOrderSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }
}
